<?php

namespace app\admin\model;

use think\Model;


class UserConfigModel extends Model
{


    // 表名
    protected $name = 'user_config';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;


    // 追加属性
    protected $append = [

    ];

    public function getDepositTypeList()
    {
        return [
            1 => '押一',
            2 => '押二',
            3 => '押三',
            4 => '押四',
            5 => '固定',
        ];
    }

    public function getList($type = null)
    {
        $config = [
            'general' => [
                [
                    'name' => '押金类型',
                    'field' => 'deposit_type',
                ],
                [
                    'name' => '固定押金',
                    'field' => 'deposit_money',
                ],
                [
                    'name' => '需要抄表',
                    'field' => 'need_cb',
                ],
                [
                    'name' => '统一交租日',
                    'field' => 'rent_day_switch',
                ],
                [
                    'name' => '交租日',
                    'field' => 'rent_day',
                ],
                [
                    'name' => '提前X天交租',
                    'field' => 'ahead_day',
                ],
                [
                    'name' => '短信通知',
                    'field' => 'sms_notice',
                ],
                [
                    'name' => '关注欢迎词',
                    'field' => 'welcome',
                ],
                [
                    'name' => '备份邮箱',
                    'field' => 'backup_email',
                ],
                [
                    'name' => '备份时间',
                    'field' => 'backup_time',
                ],
            ],
            'contract' => [
                [
                    'name' => '租期1个月及以内，到期前',
                    'field' => 'one_month',
                ],
                [
                    'name' => '租期6个月及以内，到期前',
                    'field' => 'six_month',
                ],
                [
                    'name' => '租期6个月以上，到期前',
                    'field' => 'gt_six_month',
                ],
                [
                    'name' => '自动续租',
                    'field' => 'auto_pay',
                ],
                [
                    'name' => '到期前',
                    'field' => 'ahead_auto_pay',
                ],
            ],
            'bill' => [
                [
                    'name' => '包含押金',
                    'field' => 'include_deposit',
                ],
                [
                    'name' => '自动生成',
                    'field' => 'auto_generate',
                ],
                [
                    'name' => '金额保留',
                    'field' => 'amount_decimal',
                ],
                [
                    'name' => '余额抵扣',
                    'field' => 'balance_deduction',
                ],
                [
                    'name' => '默认备注',
                    'field' => 'remark',
                ],
            ],

        ];

        return $type ? ($config[$type] ?? []) : $config;
    }


    public function getUserConfig($user_id, $type, $field = null)
    {
        if (is_null($field)) {
            return $this->where([
                'user_id' => $user_id,
                'type' => $type,
            ])->column('value', 'field');
        } else {
            return $this->where([
                'user_id' => $user_id,
                'type' => $type,
                'field' => $field,
            ])->value('value');
        }
    }


    public function saveUserConfig($user_id, $type, $data)
    {
        foreach ($data as $k => $v) {
            $row = self::get(['user_id' => $user_id, 'type' => $type, 'field' => $k]);
            if (!$row) {
                $config = $this->getList($type);
                $config = array_column($config, 'name', 'field');
                self::create([
                    'user_id' => $user_id,
                    'type' => $type,
                    'name' => $config[$k] ?? '',
                    'field' => $k,
                    'value' => $v,
                ]);
            } else {
                $row->value = $v;
                $row->save();
            }

        }
    }


    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getUpdateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getDeleteTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['delete_time']) ? $data['delete_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setCreateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setUpdateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setDeleteTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


}
